Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

234
Visualizações
does "object" in kotlin get garbage collected

If we have an Object like this

object Repo { var activeMovies: ArrayList<Movie>? = null }

and then we call it like this to assign a value

Repo.activeMovies = movieList

after the Activity that instantiated it is finish, does it get Garbage Collected?

I know this may be very basic question but I cannot understand the lifecycle if the object type in Kotlin.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If we create an object like this:

object Test {
    // some functions and properties
}

and decompile it to Java, we will see next code:

public final class Test {
    public static final Test INSTANCE;

   static {
      Test var0 = new Test();
      INSTANCE = var0;
   }
}

From the decompiled code, we can see that object creates a Singleton. The initialization happens on a static block. In Java, static blocks are executed on class loading time. The instance of Test class is created at the moment when the classloader loads the class. This approach guarantees lazy-loading and thread-safety. An instance of a singleton object is kept in a static field inside the class of that object. Therefore it isn’t eligible to the garbage collection. The Test is a Singleton, whose lifespan is as long as the lifespan of an app.

Here are some useful information about static variables Android static object lifecycle and static variable null when returning to the app.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda